//
//  HomeSearchViewController.m
//  CarUser
//
//  Created by 王森林 on 2018/12/6.
//  Copyright © 2018年 王森林. All rights reserved.
//

#import "HomeSearchViewController.h"
#import "HomeSearchHotView.h"
#import "ChooseResultViewController.h"
@interface HomeSearchViewController()<UITextFieldDelegate>
@property  (nonatomic, strong) HomeSearchHotView *hotView;
@property (nonatomic, strong) NSArray *hotAry;

@property (strong, nonatomic)  UIView *titleView;
@property (strong, nonatomic) UIView *searchView;
@property (strong, nonatomic) UITextField *searchField;

@end

@implementation HomeSearchViewController


- (BOOL )fd_prefersNavigationBarHidden{
    return YES;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self createNavUI];
    [self fetchDataFromNet];

   
}

 - (void)fetchDataFromNet
{
    
    //获取重点车型
    NSDictionary *params1 = @{
                              @"FKEY":[CommonMethod createMD5Key:@"NULL"],
                              @"SFRX00":@"1"
                              
                              };
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD showWithStatus:nil];
    });
    
    [[ZY_AfnNetManager shareInstance]postWithURL:Car_API_9 params:params1 md5Paraname:nil success:^(id  _Nullable response) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
        NSDictionary *resultDict  = response;
        self.hotAry  = [resultDict objectForKey:@"list"];
         [self createUI];
        
    } fail:^(NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
        });
        
    }];
    
    
}


#pragma mark - 搜索按钮
- (void)handleSearch
{
//    if (self) {
//        <#statements#>
//    }
    if ([CommonMethod isBlankString:self.searchField.text]) {
          [SVProgressHUD showErrorWithStatus:@"请输入关键字"];
         return;
    }
    ChooseResultViewController  *controller  = [[ChooseResultViewController alloc] init];
     controller.pushType = 1;
    controller.keywords = self.searchField.text;
    [self.navigationController pushViewController:controller animated:YES];
    
    
}

- (void)createUI
{
//    HomeSearchHotView *hotView = [[HomeSearchHotView alloc] initWithFrame:CGRectMake(0, 0, BOUNDWIDTH, BOUNDHEIGHT) WithData:self.hotAry];
//    hotView.titleName  = @"热门搜索";
    [self.view addSubview:self.hotView];
}


- (void)backAction:(id)sender
{
    [self.view endEditing:YES];
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark - UI相关
- (void)createNavUI{
    

    
    if (!self.titleView) {
        self.titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, BOUNDWIDTH, IS_IPHONE_X?88:64)];
        self.titleView.backgroundColor = Common_NavigroundColor;
        [self.view addSubview:self.titleView];
    }
  
    UIImage *backImage = IMG(@"nav_back");
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(15,IS_IPHONE_X?44:20 , IMGWIDTH(backImage),30);
    
    [backBtn setImage:backImage forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.titleView addSubview:backBtn];
//    [se mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(self.searchView.mas_right).offset(14);
//        make.width.mas_equalTo(34);
//        make.height.mas_equalTo(34);
//        make.centerY.mas_equalTo(self.searchView.mas_centerY);
//    }];
    
    
    self.searchView = [[UIView alloc]  initWithFrame:CGRectMake(50, IS_IPHONE_X?49:25, BOUNDWIDTH-110, 30)];
    self.searchView.backgroundColor = [UIColor whiteColor];
    self.searchView.layer.cornerRadius =15;
    self.searchView.layer.masksToBounds = YES;
    [self.titleView addSubview:self.searchView];
    
    UIImage *searchImage = IMG(@"home_search");
    UIImageView *searchImageView = [[UIImageView alloc] initWithImage:searchImage];
    [self.searchView addSubview:searchImageView];
    [searchImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.searchView.mas_left).offset(8);
        make.width.mas_equalTo(IMGWIDTH(searchImage));
        make.height.mas_equalTo(IMGHEIGHT(searchImage));
        make.centerY.mas_equalTo(self.searchView.mas_centerY);
    }];
   
    
    
    
    self.searchField = [UITextField new];
    self.searchField.placeholder = @"搜索品牌/车型/经销商";
    self.searchField.font = FONT_SIZE(12);
//    self.searchField.textColor = Common_TextLowColor;
    [self.searchField setValue:Common_TextLowColor forKeyPath:@"_placeholderLabel.textColor"];
    self.searchField.delegate = self;
    [self.searchView addSubview: self.searchField];
    [self.searchField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(searchImageView.mas_right).offset(9);
        make.right.mas_equalTo(self.searchView.mas_right).offset(-8);
        make.height.mas_equalTo(30);
        make.centerY.mas_equalTo(self.searchView.mas_centerY);
    }];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(BOUNDWIDTH - 50, 20, 40, 30);
    btn.titleLabel.font = FONT_SIZE(14);
    [btn setTitle:@"搜索" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setTitleColor:Common_TextColor forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(handleSearch) forControlEvents:UIControlEventTouchUpInside];
    [self.titleView addSubview:btn];
    
    backBtn.centerY  = self.searchView.centerY;
    btn.centerY = self.searchView.centerY;
    
    
//    self.navigationController.navigationBar.translucent = NO;
//    self.navigationController.navigationBar.shadowImage = [UIImage new];
//    
//    CGFloat navHeight = 44;
//    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, BOUNDWIDTH- 120, navHeight)];
//
//    UIView *searchView = [[UIView alloc]  initWithFrame:CGRectMake(0, 5, BOUNDWIDTH- 130, 30)];
//    searchView.backgroundColor = [UIColor whiteColor];
//    searchView.layer.cornerRadius = 15;
//    searchView.layer.masksToBounds = YES;
//    [titleView addSubview:searchView];
//
//    UIImage *searchImage = IMG(@"home_search");
//    UIImageView *searchImageView = [[UIImageView alloc] initWithImage:searchImage];
//    [searchView addSubview:searchImageView];
//    searchImageView.frame = CGRectMake(8, (30-IMGHEIGHT(searchImage))/2, IMGWIDTH(searchImage), IMGHEIGHT(searchImage));
//
//    UITextField *searchField = [UITextField new];
//    searchField.placeholder = @"搜索车辆、车辆和配件和服务信息";
//    [searchField setValue:Common_TextLowColor forKeyPath:@"_placeholderLabel.textColor"];
//    [searchField setValue:FONT_SIZE(12) forKeyPath:@"_placeholderLabel.font"];
//    searchField.delegate = self;
//    [searchView addSubview:searchField];
//    searchField.frame = CGRectMake(8+ IMGWIDTH(searchImage)+8, 0, BOUNDWIDTH - 130 - IMGWIDTH(searchImage) - 16,30);
//
//    
//    NSString *title  = @"搜索";
//    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
////    NSDictionary *attr=@{NSFontAttributeName:FONT_SIZE(12)};
////    CGSize titleSize = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context: nil].size;
////    btn.frame = CGRectMake(BOUNDWIDTH - 50-20 - titleSize.width-10, 5, titleSize.width+20, 30);
//    btn.titleLabel.font = FONT_SIZE(12);
//    [btn setTitle:title forState:UIControlStateNormal];
//    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [btn setTitleColor:Common_TextColor forState:UIControlStateHighlighted];
//    [btn addTarget:self action:@selector(handleSearch) forControlEvents:UIControlEventTouchUpInside];
//    [titleView addSubview:btn];
//    self.navigationItem.titleView = titleView;
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];

   

    
 
    
}

- (HomeSearchHotView *)hotView
{
    if (!_hotView) {
        _hotView = [[HomeSearchHotView alloc] initWithFrame:CGRectMake(0, IS_IPHONE_X?88:64, BOUNDWIDTH, BOUNDHEIGHT-(IS_IPHONE_X?88:64)) WithData:self.hotAry];
        _hotView.titleName  = @"热门搜索";
    }
    return _hotView;
}

//- (NSArray *)hotAry
//{
//    if (!_hotAry) {
//        _hotAry = [[NSArray  alloc] initWithObjects:@"奔驰gle400",@"奔跑者",@"坦途",@"揽胜运动3.0柴油",@"lebante",@"霸道4000",@"奔驰gle400",@"奔跑者",@"坦途",@"揽胜运动3.0柴油",@"lebante",@"霸道4000", nil];
//    }
//    return _hotAry;
//
//}

@end
